package com.example.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.system.entity.Remind;
import com.example.system.exception.CustomException;
import com.example.system.mapper.RemindMapper;
import com.example.system.service.IRemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RemindServiceImpl extends ServiceImpl<RemindMapper, Remind> implements IRemindService {
    @Autowired
    RemindMapper remindMapper;

    public int deleteRemindByRemindId(Integer remindId) {
        return remindMapper.deleteRemindByRemindId(remindId);
    }

    public void addRemindByRemind(Remind remind) {
        //类型一定要有不能为空
        if (remind.getType() == null || remind.getType() == 0) {
            throw new CustomException("类型不能为空");
        }
        //时间一定要有不能为空
        if (remind.getRemindTime() == null) {
            throw new CustomException("时间不能为空");
        }
        remindMapper.addRemindByRemind(remind);
    }


    public void updateRemindByRemind(Remind remind) {
        if (remind == null) {
            throw new IllegalArgumentException("Remind 对象不能为空");
        }
        Remind usedRemind = remindMapper.findRemindByRemindID(remind.getRemindId());
        if (usedRemind == null) {
            throw new CustomException("未找到对应的 Remind 条目");
        }
        if (!remind.getUserId().equals(usedRemind.getUserId())) {
            throw new CustomException("当前请求修改的用户与原用户的用户名不同");
        }
        remindMapper.updateRemindByRemindId(remind.getRemindId());
    }


    public Remind findRemindByRemindId(Integer remindId) {
        return remindMapper.findRemindByRemindID(remindId);
    }

}
